<?php
if(isset($_REQUEST['async']) && $_REQUEST['async']==1){
	include_once("/var/www/html/new/com.inc.php");
	include_once '/var/www/html/new/d/class/Member.php';
	include_once '/var/www/html/new/d/class/abc/Product.class.php';
	include_once '/var/www/html/new/d/common/area.inc.php';
	$ac = $GLOBALS['_ac'];
	$pw = $GLOBALS['_pw'];
	$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

	$memberSID = $_SESSION['sid'];

	// 获取数据
	$mem   = new Member($s, $memberSID);
	$prods = $mem->salePros();// 获取门票

	// 是否可打包现场支付
	$saleScene = false;
	$sql = "select group_id from pft_member where id=$memberSID limit 1";
	$GLOBALS['le']->query($sql);
	$GLOBALS['le']->fetch_assoc();
	$group_id = $GLOBALS['le']->f('group_id');
	if($group_id==4) $saleScene = true;// 云顶允许打包现场支付
	if($_SESSION['sid']==2148) $saleScene = true;// 云顶允许打包现场支付

	// 景区模糊查询
	$lids = array();
	if(isset($_REQUEST['title'])){
		$ltitle = $_REQUEST['title'];
		$sql = "select id from uu_land where title like '%$ltitle%'";
		$GLOBALS['le']->query($sql);
		while($row=$GLOBALS['le']->fetch_assoc()) $lids[] = $row['id'];
	}
	$lids_count = count($lids);

	// 获取供应商名称
	$supplier = array();
	$sql = "select id,dname from pft_member where dtype=0";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()) $supplier[$row['id']] = $row['dname'];
	
	// 获取城市地址库
	$areas = array();
	$sql = "select area_id,area_name from uu_area";
	$GLOBALS['le']->query($sql);
	while($row=$GLOBALS['le']->fetch_assoc()) $areas[$row['area_id']] = $row['area_name'];

	// 分页依据
	$pageSize = isset($_REQUEST['pageSize']) ? $_REQUEST['pageSize']+0:2;
	$lastRows = isset($_REQUEST['last']) ? $_REQUEST['last']:'';// 9082_3614 lid_aid
	$provice  = isset($_REQUEST['provice']) ? $_REQUEST['provice']:'';
	$city     = isset($_REQUEST['city']) ? $_REQUEST['city']:'';

	if($lastRows){
		$lastRows = explode('_', $lastRows);
		$last_lid = $lastRows[0];
		$last_aid = $lastRows[1];
	}

	$buoy = 0;// 数据浮标
	$count = 0;
	$today = date('Y-m-d'); 
	$lastRow = '';
	$prodRow = array();
	foreach($prods as $key=>$row){
		
		// 已经找到需要的景区个数，并且不是该供应商供应的门票
		// if($lastRow!='' && $lastRow!=($row['lid'].'_'.$row['sapply_did'])) continue;
		if($lastRow!='' && !in_array($row['lid'].'_'.$row['sapply_did'], $prodRow)) continue;
		
		
		if($lastRows){// 第二页及以上
			if($row['lid']==$last_lid && $row['sapply_did']==$last_aid){
				$buoy = ($key+1); $lastRows = '';
			}else{
				continue;
			}
		}
		
		if($key<$buoy) continue;
		
		if($lids_count && !in_array($row['lid'], $lids)) continue;
		if($row['p_type']=='F' || $row['p_type']=='B' || $row['p_type']=='H' || $row['sync_status']==2) continue;
		if($row['pay']==0 && !$saleScene) continue;// 现场支付允许打包
		
		// 城市过滤
		$areas_arr = explode('|',$row['area']);
		if($city!='' && $areas_arr[1]!=$city) continue;
		if($provice!='' && $areas_arr[0]!=$provice) continue;
		
		// 获取价格
		$price = abc777\Product::Price($s, $row['pid'], $row['sapply_did'], $today);
		
		if($price['js']==-1) continue;
		if(!isset($save[$row['lid'].'_'.$row['sapply_did']])){
			$land['lid']        = $row['lid'];
			$land['title']      = $row['title'];
			$land['p_type']     = $row['p_type'];
			$land['apply_dname']      = $supplier[$row['sapply_did']];
			$land['areas']      = $areas_arr;
			$land['address']    = $areas[$areas_arr[1]];
			$land['sapply_did'] = $row['sapply_did'];
			$save[$row['lid'].'_'.$row['sapply_did']] = $land;
			$count = $count + 1;
			$prodRow[] = $row['lid'].'_'.$row['sapply_did'];
		}
		
		$ticket['lid']     = $row['lid'];
		$ticket['tid']     = $row['tid'];
		$ticket['aid']     = $row['sapply_did'];
		$ticket['pid']     = $row['pid'];
		$ticket['title']   = $row['ttitle'];
		$ticket['jsprice'] = $price['js'];
		$ticket['uprice']  = $price['ls'];
		$save[$row['lid'].'_'.$row['sapply_did']]['ticket'][]=$ticket;
		if($lastRow=='' && $count>=$pageSize) $lastRow = $row['lid'].'_'.$row['sapply_did'];
	}

	$response = array( 'lists'=>$save, 'last'=>$lastRow, 'code'=>200 );
	echo json_encode($response);
	exit;
}
include_once '/var/www/html/new/com.inc.php';
include_once '/var/www/html/new/d/common/autoload.php';
include_once '/var/www/html/new/d/common/header.html';
include_once '/var/www/html/new/d/common/left.html';
include_once '/var/www/html/new/d/class/Member.php';
include_once '/var/www/html/new/d/tpl/product_package/product_package.html';
include_once '/var/www/html/new/d/common/footer.html';

